package org.raymond.iworks.study.netty.official.fundamental.time2;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
public class TimeDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { // (2)
        if (in.readableBytes() < 4) {
            return;
        }
        UnixTime ut = new UnixTime(in.readUnsignedInt());
        log.info("client decode: {}", ut);
        out.add(ut);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        log.error("decoder exceptionCaught...", cause);
        ctx.close();
    }
}
